// 处理混合错误的方式是，让他们相互拥有对方
// 不是option 拥有Result，就是Result 拥有option


// 下面是Option拥有Result的例子

use std::num::ParseIntError;

fn double_first(vec: Vec<&str>) -> Option<Result<i32, ParseIntError>> {
    vec.first().map(|first| {
        first.parse::<i32>().map(|n| 2 * n)
    })
}

fn double_first_result(vec: Vec<&str>) -> Result<Option<i32>, ParseIntError> {
    let opt = vec.first().map(|first| {
        first.parse::<i32>().map(|n| 2 * n)
    });
    // 将Option转为Result，或Result转Option
    opt.transpose()
}

fn main() {
    let numbers = vec!["42", "93", "18"];
    let empty = vec![];
    let strings = vec!["tofu", "93", "18"];

    println!("The first doubled is {:?}", double_first(numbers.clone()));

    println!("The first doubled is {:?}", double_first(empty.clone()));
    // Error 1: the input vector is empty

    println!("The first doubled is {:?}", double_first(strings.clone()));
    // Error 2: the element doesn't parse to a number

    println!("-------------- double_first_result ---------------------------");

    println!("The first doubled is {:?}", double_first_result(numbers));
    println!("The first doubled is {:?}", double_first_result(empty));
    println!("The first doubled is {:?}", double_first_result(strings));

}